package com.xyh.controller;

import com.xyh.entity.YxcUser;
import com.xyh.service.YxcLoginService;
import com.xyh.service.YxcUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class YxcUserController {

    @Resource
    private YxcUserService yxcUserService;

    @Resource
    private YxcLoginService yxcLoginService;

    @RequestMapping("/to_yxc_user_update")
    public String toYxcUpdate(HttpServletRequest request, Model model){
        String login_telephone = request.getParameter("login_telephone");
        YxcUser yxcUser = yxcUserService.selectByTelephone(login_telephone);
        model.addAttribute("yxcUser",yxcUser);
        model.addAttribute("login_telephone",login_telephone);
        return "yxc_user_update";
    }

    @RequestMapping("/update_yxc_user")
    public String updateYxcUser(HttpServletRequest request, RedirectAttributes attributes,YxcUser yxcUser){
        String login_telephone = request.getParameter("telephone");
        int flag = yxcUserService.updateYxcUser(yxcUser);
        int res = yxcLoginService.updateYxcLogin(yxcUser);
        if (flag == 0 || res == 0) throw new RuntimeException("修改失败");
        attributes.addAttribute("login_telephone",login_telephone);
        attributes.addAttribute("situation",0);
        return "redirect:to_yxc_main";
    }
}
